]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/SearchBar.swift
Address the lint warnings
[rbdr/map] / Map / Presentation / Base Components / SearchBar.swift
1 // Copyright (C) 2024 Rubén Beltrán del Río
2
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see https://map.tranquil.systems.
15 import SwiftUI
16
17 struct SearchBar: View {
18
19 @Binding var term: String
20 @FocusState var isSearchFocused: Bool
21 var onNext: () -> Void = {}
22 var onPrevious: () -> Void = {}
23 var onSubmit: () -> Void = {}
24 var onDismiss: () -> Void = {}
25
26 var body: some View {
27 HStack(spacing: 2.0) {
28 ZStack {
29 TextField("Search", text: $term)
30 .textFieldStyle(.roundedBorder)
31 .padding(.trailing, 16.0)
32 .focused($isSearchFocused)
33 .onAppear {
34 isSearchFocused = true
35 }
36 .onKeyPress { press in
37 if press.key == .return {
38 if press.modifiers.contains(.shift) {
39 onPrevious()
40 return .handled
41 }
42 onNext()
43 return .handled
44 }
45 return .ignored
46 }
47 }
48 Spacer()
49 Button(action: onPrevious) {
50 Image(systemName: "chevron.left")
51 .font(.Theme.smallControl)
52 }.keyboardShortcut(
53 "g", modifiers: EventModifiers([.command, .shift])
54 ).help("Find Previous (⇧⌘G)")
55 Button(action: onNext) {
56 Image(systemName: "chevron.right")
57 .font(.Theme.smallControl)
58 }.keyboardShortcut(
59 "g", modifiers: EventModifiers([.command])
60 ).help("Find Next (⌘G)")
61 Button(action: onDismiss) {
62 Text("Done")
63 .font(.Theme.smallControl)
64 }.keyboardShortcut(.escape, modifiers: EventModifiers())
65 .help("Done (⎋)")
66 }.padding(4.0)
67 }
68 }
69
70 #Preview {
71 SearchBar(term: .constant("Hello"))
72 }